package com.fp.m7.client.view;

import com.fp.m7.client.MyMessages;
import com.fp.m7.client.common.ProfileTab;
import com.fp.m7.client.presenter.ProfilePresenter;
import com.fp.m7.client.widget.AccountInfoWidget;
import com.fp.m7.client.widget.ListContainerWithPrevNextButtonsWidget;
import com.fp.m7.client.widget.UserListContainerWidget;
import com.fp.m7.shared.dto.AccountDto;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.TabLayoutPanel;

/**
 * Profile view. Right side contains some account information, the left side contains a tabbed panel with three tabs:
 * Comments, Followers and Following.
 * 
 * @author 7
 */
public class ProfileView extends Composite implements ProfilePresenter.Display {

    private static MyMessages msg = GWT.create(MyMessages.class);

    private TabLayoutPanel tabLayoutPanel;

    private AccountInfoWidget accountInfo;
    private ListContainerWithPrevNextButtonsWidget commentContainer;
    private UserListContainerWidget followersContainer;
    private UserListContainerWidget followingContainer;

    public ProfileView() {
        DockPanel dockPanel = new DockPanel();
        initWidget(dockPanel);
        dockPanel.setSize("100%", "100%");

        accountInfo = new AccountInfoWidget();
        dockPanel.add(accountInfo, DockPanel.EAST);

        tabLayoutPanel = new TabLayoutPanel(2.5, Unit.EM);
        tabLayoutPanel.setAnimationDuration(1000);
        tabLayoutPanel.setSize("468px", "100%");
        tabLayoutPanel.addStyleName("profileTab");
        dockPanel.add(tabLayoutPanel, DockPanel.WEST);

        commentContainer = new ListContainerWithPrevNextButtonsWidget();
        commentContainer.getContentPanel().getEmptyLabel().setText(msg.noCommentsYet());
        tabLayoutPanel.add(commentContainer, msg.comments());

        followersContainer = new UserListContainerWidget();
        followersContainer.getContentPanel().getEmptyLabel().setText(msg.noFollowers());
        tabLayoutPanel.add(followersContainer, msg.followers());

        followingContainer = new UserListContainerWidget();
        followingContainer.getContentPanel().getEmptyLabel().setText(msg.noFollowing());
        tabLayoutPanel.add(followingContainer, msg.following());
    }

    @Override
    public void setAccountData(AccountDto account) {
        accountInfo.setData(account);
    }

    @Override
    public HasWidgets getCommentsPanel() {
        return commentContainer.getContentPanel();
    }

    @Override
    public HasWidgets getFollowersPanel() {
        return followersContainer.getContentPanel();
    }

    @Override
    public HasWidgets getFollowingPanel() {
        return followingContainer.getContentPanel();
    }

    @Override
    public HasClickHandlers getPreviousButton() {
        return commentContainer.getPreviousButton();
    }

    @Override
    public HasClickHandlers getNextButton() {
        return commentContainer.getNextButton();
    }

    @Override
    public void setCanFollow(Boolean mode) {
        accountInfo.getFollowToggleButton().setVisible(mode != null);

        if (mode == null) {
            return;
        }

        accountInfo.getFollowToggleButton().setDown(!mode);
    }

    @Override
    public HasClickHandlers getFollowButton() {
        return accountInfo.getFollowToggleButton();
    }

    @Override
    public HasValue<Boolean> getFollowButtonMode() {
        return accountInfo.getFollowToggleButton();
    }

    @Override
    public void focusOnTab(ProfileTab tab) {
        if (tab != null) {
            switch (tab) {
            case COMMENTS:
                tabLayoutPanel.selectTab(0);
                break;
            case FOLLOWERS:
                tabLayoutPanel.selectTab(1);
                break;
            case FOLLOWING:
                tabLayoutPanel.selectTab(2);
                break;
            }
        }
    }
}
